//
//  HttpManager.m
//  hotlifeForBusiness
//
//  Created by zhuweideng on 16/3/29.
//  Copyright © 2016年 zhuweideng. All rights reserved.
//

#import "HttpManager.h"
#define SUCCESS @"200"      /* 请求成功 */
#define FAIL @"500"         /* 系统错误 */
#define LOSTTOKEN @"417"    /*  token过期 */

static HttpManager *instance;

@implementation HttpManager

+ (HttpManager *)getInstance{
    if (instance == nil) {
        instance = [[self allocWithZone:NULL]init];
    }
    return instance;
}

- (void)post:(NSString *)url params:(NSDictionary *)parameters successCallback:(void (^)(NSString *state, id responseObject))successCallback{
    
//    if (![self isNetworkEnabled]) {
//        [vc showHint:@"当前无网络"];
//    }
    
    //创建Session manager
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
   
    //创建request, POST请求方法
    NSURLRequest *request = [[AFHTTPRequestSerializer serializer]requestWithMethod:@"POST" URLString:url parameters:parameters error:nil];
    
    NSURLSessionDataTask *task = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            DLog(@"接口－%@－%@－接口访问失败 %@",url,parameters,error);
        }else{
            DLog(@"接口－%@－%@－接口访问成功 %@",url,parameters,responseObject);
            /* 获取状态码 */
             NSString *state = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"op_code"]];
            
            /* 这里进行一系列错误状态码判断 */
            
            if ([state isEqualToString:@"Y"]) {
                if (successCallback)
                    successCallback(state, responseObject);
            }
        }
    }];
    
    [task resume];
    
    
    
}
- (void)get:(NSString *)url params:(NSDictionary *)parameters successCallback:(void (^)(NSString *state, id responseObject))successCallback{
    
    //创建Session manager
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    //创建request, get请求方法
    NSURLRequest *request = [[AFHTTPRequestSerializer serializer]requestWithMethod:@"GET" URLString:url parameters:parameters error:nil];
    
    NSURLSessionDataTask *task = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            DLog(@"接口－%@－%@－接口访问失败 %@",url,parameters,error);
        }else{
            DLog(@"接口－%@－%@－接口访问成功 %@",url,parameters,responseObject);
            /* 获取状态码 */
            NSString *state = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"op_code"]];
            
            /* 这里进行一系列错误状态码判断 */
            
            if ([state isEqualToString:@"Y"]) {
                if (successCallback)
                    successCallback(state, responseObject);
            }
        }
    }];
    
    [task resume];

}
- (void)post:(NSString *)url params:(NSDictionary *)parameters successCallback:(void (^)(NSString *state, id responseObject))successCallback failCallback:(void (^)(NSString *state, id responseObject))failCallback{

//    if (![self isNetworkEnabled]) {
//        [vc showHint:@"当前无网络"];
//    }
    
    //创建Session manager
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    //创建request,post请求方法
    NSURLRequest *request = [[AFHTTPRequestSerializer serializer]requestWithMethod:@"POST" URLString:url parameters:parameters error:nil];
    
    NSURLSessionDataTask *task = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            DLog(@"接口－%@－%@－接口访问失败 %@",url,parameters,error);
        }else{
            DLog(@"接口－%@－%@－接口访问成功 %@",url,parameters,responseObject);
            /* 获取状态码 */
            NSString *state = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"op_code"]];
            
            /* 这里进行一系列错误状态码判断 */
            
            if ([state isEqualToString:@"Y"]) {
                if (successCallback)
                    successCallback(state, responseObject);
            }else{
                if (failCallback)
                    failCallback(state, responseObject);
            }
        }
    }];
    
    [task resume];
    
    
}
//返回JSON
- (void)get:(NSString *)url params:(NSDictionary *)parameters successCallback:(void (^)(NSString *state, id responseObject))successCallback failCallback:(void (^)(NSString *state, id responseObject))failCallback{
    
    //    if (![self isNetworkEnabled]) {
    //        [vc showHint:@"当前无网络"];
    //    }
    
    //创建Session manager
    AFHTTPSessionManager *mag=[AFHTTPSessionManager manager];
    mag.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    //创建request,get请求方法

    NSURLRequest *request = [[AFHTTPRequestSerializer serializer]requestWithMethod:@"GET" URLString:url parameters:parameters error:nil];
    NSURLSessionDataTask *task = [mag dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            DLog(@"接口－%@－%@－接口访问失败 %@",url,parameters,error);
            failCallback(@"error",responseObject);
        }else{
            DLog(@"接口－%@－%@－接口访问成功 %@",url,parameters,responseObject);
            /* 获取状态码 */
            NSString *state = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"status"]];
            
            /* 这里进行一系列错误状态码判断 */
            
            if ([state isEqualToString:@"0"]) {
                if (successCallback)
                    successCallback(state, responseObject);
            }else{
                if (failCallback)
                    failCallback(state, responseObject);
            }
        }
    }];
    
    [task resume];
    
    
}

//返回字符串
- (void)getsStockData:(NSString *)url params:(NSDictionary *)parameters responseObjects:(void (^)( id responseObject))responseObjects failCallback:(void (^)(NSString *state, id responseObject))failCallback{
    
    
    //创建Session manager
    AFHTTPSessionManager *mag=[AFHTTPSessionManager manager];
    mag.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",nil];


    //创建request, get请求方法
    NSURLRequest *request = [[AFHTTPRequestSerializer serializer]requestWithMethod:@"GET" URLString:url parameters:parameters error:nil];
    


   
    NSURLSessionDataTask *task = [mag dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            DLog(@"接口－%@－%@－接口访问失败 %@",url,parameters,error);
             failCallback(@"error",responseObject);
        }else{
            DLog(@"接口－%@－%@－接口访问成功 %@",url,parameters,responseObject);
            responseObjects(responseObject);
        }
       
        
    }];
    
    [task resume];
    
}
//-(NSString*) rsaEncryptString:(NSString*)string
//{
//    NSString *pubkey = @"-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCZdAVB7dsods20GXPruSHrdqlk\npktzBZ3i8vw8h2ZCpLQ6wd0Z4XqjGH8z0VXXrdiNYVP0vOLJAu4BtjSI2NnkFIQ+\nRgJ4PTDIW3W+AoUDCv1Je0sA7tPoIeFbXz0EUvlwDimxn1HiA2xX1S19WiPvmC7S\nmZc+rspGQuv06o8qkQIDAQAB\n-----END PUBLIC KEY-----";
//    
//    NSString *ret = [rsa encryptString:string publicKey:pubkey];
    
    
//    return ret;
//}
- (void)dealloc{
    CFRelease(publicKey);
}




/* unicode码转中文 */
- (NSString *)replaceUnicode:(NSString *)unicodeStr{
    
    NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:@"\\u"withString:@"\\U"];
    NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""];
    NSString *tempStr3 = [[@"\""stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData
                                                           mutabilityOption:NSPropertyListImmutable
                                                                     format:NULL
                                                           errorDescription:NULL];
    return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n"withString:@"\n"];
                        
}


@end
